const LogModel = require('../../model/Log');
const moment = require('moment')
const ShowActivity = require('../../model/ShowActivity')

module.exports = {

    //获取日志列表
    changeShowActivity: async(req, res, next) => {
        try {
            let showActivity = await ShowActivity.findOne({ page: req.body.page }).exec()
            if (showActivity == null) {
                let newShowActivity = new ShowActivity({
                    page: req.body.page,
                    updatedAt: new Date(),
                    image: req.body.image, // link of cdn images
                    isDelete: 0,
                    activity: req.body.activity, // 小程序内跳转链接或者网页链接,
                })
                await newShowActivity.save()
                return res.ok('创建成功', newShowActivity)
            } else {
                showActivity.updatedAt = new Date()
                showActivity.page = req.body.page
                showActivity.image = req.body.image
                showActivity.activity = req.body.activity
                await showActivity.save()
                return res.ok('修改成功', showActivity)
            }
        } catch (e) {
            next(e)
        }
    },

    getShowActivity: async(req, res, next) => {
        try {
            let showActivity = await ShowActivity.findOne({ page: req.body.page }).populate('activity').exec()
            return res.ok('查询成功', showActivity)
        } catch (e) {
            next(e)
        }
    },

    sortHomePageActivity: async(req, res, next) => {
        try {
            let showActivity = await ShowActivity.findOne({ page: "index" }).exec()
            showActivity.activity = req.body.activity
            await showActivity.save()
            return res.ok('排序成功', showActivity)
        } catch (e) {
            next(e)
        }
    },
    deleteHomePageActivity: async(req, res, next) => {
        try {
            let showActivity = await ShowActivity.findOne({ page: "index" }).exec()
            let index = showActivity.activity.indexOf(req.body.id)
            if (index != -1) {
                showActivity.activity.splice(index, 1)
                showActivity.markModified('activity')
                await showActivity.save()
                return res.ok('删除成功', showActivity)
            } else {
                return res.ok('未找到该活动')
            }
        } catch (e) {
            next(e)
        }
    },

};